package com.test;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class NotSafeDemo
{
    public static void main(String[] args)
    {
        notSafe02();
    }

    public static void notSafe01()
    {
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 30; i++)
        {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(list);
            }, "Thread " + i).start();
        }
    }

    public static void notSafe02()
    {
        List<String> list = new ArrayList();
        for (int i = 0; i < 30; i++)
        {//开启3个线程
            new Thread(() -> {
                String substring = UUID.randomUUID().toString().substring(0, 8);
                if (substring == null)
                {//确保substring不为null
                    substring = "88888888";
                }
                list.add(Thread.currentThread().getName() + "--" + substring);
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }

}